#ifndef __MUTEX_H__
#define __MUTEX_H__

typedef volatile struct ips_node_s ips_node_t;

struct ips_node_s
{
	 struct
	 {
		  unsigned int wait    : 1;
		  unsigned int ac_wait : 1;
		  unsigned int fiber   : 30;
	 } __attribute__ ((packed));
	 ips_node_t *prev, *next;
} __attribute__((aligned (4)));

struct mutex_s
{
	 spin_lock_t lock;
	 uint32_t    wait_count;
	 struct
	 {
		  unsigned int hold : 1;
		  unsigned int wait : 1;
		  unsigned int head : 30;
	 } __attribute__((packed));
} __attribute__ ((aligned (4)));

typedef volatile struct mutex_s mutex_t;

#include <fiber/fiber.h>

void mutex_init(mutex_t *mutex);
int  mutex_try_acquire(mutex_t *mutex);
int  mutex_acquire(mutex_t *mutex, ips_node_t *node, fiber_t fiber);
void mutex_ac_break(mutex_t *mutex, ips_node_t *node);
void mutex_release(mutex_t *mutex);

#endif
